﻿using System;
using System.Runtime.Serialization;
using Dynamic.Core.Types;
using Dynamic.Core.Utility;
using Dynamic.Core.Extentions;

namespace Dynamic.Core.Types
{
    /// <summary>
    /// 对应数据库列
    /// </summary>
    [DataContract]
    public class Column
    {
        private string idField = Guid.Empty.ToString();
        private string nameField = string.Empty;
        private string memoField = string.Empty;
        private string displayValueNameField = string.Empty;
        private string descrField = string.Empty;
        private string columnIDField = string.Empty;
        private bool isPKField = false;
        private bool isIdentityField = false;
        private bool isTimestampField = false;
        private string dbDataTypeField = "nvarchar";
        private string dataTypeField = "string";
        private DynamicDataType dynamicDataTypeField = DynamicDataType.Nvarchar;
        private int maxLengthField = 50;
        private int precisionField = 0;
        private int scaleField = 0;
        private bool canNullField = false;
        private string defaultField = "";

        /// <summary>
        /// 主键
        /// </summary>
        [DataMember]
        public string ID
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <summary>
        /// 物理名称
        /// </summary>
        [DataMember]
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <summary>
        /// 对应类属性名
        /// </summary>
        [DataMember]
        public string CleanName
        {
            get
            {
                return NamingUtility.GetProertyName(this.nameField);
            }
        }

        /// <summary>
        /// 对应类字段名
        /// </summary>
        [DataMember]
        public string FieldName
        {
            get
            {
                return NamingUtility.GetFieldName(this.nameField);
            }
        }

        /// <summary>
        /// 备注
        /// </summary>
        [DataMember]
        public string Memo
        {
            get
            {
                return this.memoField;
            }
            set
            {
                this.memoField = value;
            }
        }

        /// <summary>
        /// 显示名
        /// </summary>
        [DataMember]
        public string DisplayValueName
        {
            get
            {
                return this.displayValueNameField;
            }
            set
            {
                this.displayValueNameField = value;
            }
        }

        /// <summary>
        /// 描述
        /// </summary>
        [DataMember]
        public string Descr
        {
            get
            {
                if (this.descrField.IsInvalid())
                {
                    return this.Name;
                }
                else
                {
                    return this.descrField;
                }
            }
            set
            {
                this.descrField = value;
            }
        }

        /// <summary>
        /// 列序号
        /// </summary>
        [DataMember]
        public string ColumnID
        {
            get
            {
                return this.columnIDField;
            }
            set
            {
                this.columnIDField = value;
            }
        }

        /// <summary>
        /// 是否主键
        /// </summary>
        [DataMember]
        public bool IsPK
        {
            get
            {
                return this.isPKField;
            }
            set
            {
                this.isPKField = value;
            }
        }

        /// <summary>
        /// 是否自增
        /// </summary>
        [DataMember]
        public bool IsIdentity
        {
            get
            {
                return this.isIdentityField;
            }
            set
            {
                this.isIdentityField = value;
            }
        }

        /// <summary>
        /// 是否时间戳
        /// </summary>
        [DataMember]
        public bool IsTimestamp
        {
            get
            {
                return this.isTimestampField;
            }
            set
            {
                this.isTimestampField = value;
            }
        }

        /// <summary>
        /// 数据库对应类型
        /// </summary>
        [DataMember]
        public string DbDataType
        {
            get
            {
                return dbDataTypeField;
            }
            set
            {
                dbDataTypeField = value;
            }
        }

        /// <summary>
        /// C#数据类型
        /// </summary>
        [DataMember]
        public string DataType
        {
            get
            {
                return this.dataTypeField;
            }
            set
            {
                this.dataTypeField = value;
            }
        }

        /// <summary>
        /// Dynamic数据类型
        /// </summary>
        [DataMember]
        public DynamicDataType DynamicDataType
        {
            get
            {
                return this.dynamicDataTypeField;
            }
            set
            {
                this.dynamicDataTypeField = value;
            }
        }

        /// <summary>
        /// 最大长度
        /// </summary>
        [DataMember]
        public int MaxLength
        {
            get
            {
                return this.maxLengthField;
            }
            set
            {
                this.maxLengthField = value;
            }
        }

        /// <summary>
        /// 精确度
        /// </summary>
        [DataMember]
        public int Precision
        {
            get
            {
                return this.precisionField;
            }
            set
            {
                this.precisionField = value;
            }
        }

        /// <summary>
        /// 小数位数
        /// </summary>
        [DataMember]
        public int Scale
        {
            get
            {
                return this.scaleField;
            }
            set
            {
                this.scaleField = value;
            }
        }

        /// <summary>
        /// 能否为空
        /// </summary>
        [DataMember]
        public bool CanNull
        {
            get
            {
                return this.canNullField;
            }
            set
            {
                this.canNullField = value;
            }
        }

        /// <summary>
        /// 默认值
        /// </summary>
        [DataMember]
        public string Default
        {
            get
            {
                return this.defaultField;
            }
            set
            {
                this.defaultField = value;
            }
        }
    }
}
